package com.leyou.page.service.impl;

import com.leyou.common.utils.BeanHelper;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.dto.SpecGroupNameDTO;
import com.leyou.page.dto.SpecParamNameDTO;
import com.leyou.page.service.GoodsPageService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 虎哥
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    private final ItemClient itemClient;

    private final StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";

    public GoodsPageServiceImpl(ItemClient itemClient, StringRedisTemplate redisTemplate) {
        this.itemClient = itemClient;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public String loadSpuData(Long spuId) {
        // 1.查询spu
        SpuDTO spu = itemClient.querySpuById(spuId);
        // 2.抽取需要的数据
        Map<String, Object> map = new HashMap<>();
        map.put("id", spu.getId());
        map.put("name", spu.getName());
        map.put("brandId", spu.getBrandId());
        map.put("categoryIds", spu.getCategoryIds());
        // 3.对象序列化为json字符串
        String json = JsonUtils.toJson(map);
        // 4.写入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + spuId, json, 24, TimeUnit.HOURS);
        // 5.返回
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        // 1.查询spuDetail
        SpuDetailDTO detail = itemClient.querySpuDetailById(spuId);
        // 3.对象序列化为json字符串
        String json = JsonUtils.toJson(detail);
        // 4.写入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL + spuId, json, 24, TimeUnit.HOURS);
        // 5.返回
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        // 1.查询
        List<SkuDTO> skuList = itemClient.querySkuBySpuId(spuId);
        // 2.对象序列化为json字符串
        String json = JsonUtils.toJson(skuList);
        // 3.写入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU + spuId, json, 24, TimeUnit.HOURS);
        // 4.返回
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        // 1.查询
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        // 2.抽取需要的数据
        List<Map<String, Object>> list = new ArrayList<>();
        for (CategoryDTO categoryDTO : categoryDTOList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            list.add(map);
        }
        // 3.对象序列化为json字符串
        String json = JsonUtils.toJson(list);
        // 4.写入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY + ids.get(ids.size() - 1), json, 24, TimeUnit.HOURS);
        // 5.返回
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        // 1.查询
        BrandDTO brand = itemClient.queryBrandById(brandId);
        // 2.抽取需要的数据
        Map<String, Object> map = new HashMap<>();
        map.put("id", brand.getId());
        map.put("name", brand.getName());
        // 3.对象序列化为json字符串
        String json = JsonUtils.toJson(map);
        // 4.写入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND + brandId, json, 24, TimeUnit.HOURS);
        // 5.返回
        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        // 1.查询
        List<SpecGroupDTO> groupList = itemClient.querySpecList(categoryId);
        // 2.抽取需要的数据
        List<SpecGroupNameDTO> groupNames = new ArrayList<>();
        for (SpecGroupDTO groupDTO : groupList) {
            // 3.创建一个SpecGroupName
            SpecGroupNameDTO groupNameDTO = new SpecGroupNameDTO();
            // 3.1.填名字
            groupNameDTO.setName(groupDTO.getName());
            // 3.2.填params
            List<SpecParamDTO> params = groupDTO.getParams();
            List<SpecParamNameDTO> paramNameDTOS = BeanHelper.copyWithCollection(params, SpecParamNameDTO.class);
            groupNameDTO.setParams(paramNameDTOS);
            // 3.3.放入集合
            groupNames.add(groupNameDTO);
        }
        // 4.对象序列化为json字符串
        String json = JsonUtils.toJson(groupNames);
        // 5.写入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPEC + categoryId, json, 24, TimeUnit.HOURS);
        // 6.返回
        return json;
    }

    @Override
    public Boolean deleteSku(Long spuId){
        return redisTemplate.delete(KEY_PREFIX_SKU + spuId);
    }
}
